package TI;

import java.util.Map;
import java.util.TreeMap;

import org.joda.time.DateTime;

import Exceptions.NotEnoughStockException;
import Magnitudes.Money;
import Products.Presentation;

public class Storehouse{

	private Address address;
	private Map<Presentation, Integer> stock;
	private Map<Presentation, Integer> minStock;
	private Map<Presentation, Integer> criticalStock;
	
	
	
	public Storehouse(Address address) {
		this.address = address;
		this.stock = new TreeMap<Presentation, Integer>();
		this.minStock = new TreeMap<Presentation, Integer>();
		this.criticalStock = new TreeMap<Presentation, Integer>();
	}

	
	
	public Map<Presentation, Integer> minimalStockPresentations(){
		return this.minStock;
	}
	
	
	public Map<Presentation, Integer> criticalStockPresentations() {		
		return this.criticalStock;
	}
	


	public boolean isAvailable(Presentation aPresentation) {
		boolean ret = false; 
		if(this.stock.containsKey(aPresentation) && (this.stock.get(aPresentation) > 0)){
			ret = true;
		}		
		return ret;
	}



	public void receive(Presentation aPresentation, int cant) {
		int actual; 
		if (this.stock.containsKey(aPresentation)){
			actual = this.stock.get(aPresentation);
			this.stock.remove(aPresentation);
		} else {
			actual = 0;
		}
		this.stock.put(aPresentation, (cant + actual));
	}


	
	public void deliver(Presentation aPresentation, int cant) throws NotEnoughStockException{
		int cantidadDePresentaciones = 0;
		if(this.stock.containsKey(aPresentation) && this.stock.get(aPresentation) >= cant){
			cantidadDePresentaciones = this.stock.get(aPresentation);
			this.stock.remove(aPresentation);
			this.stock.put(aPresentation, (cantidadDePresentaciones - cant));
		}else{
			throw new NotEnoughStockException();
		}
	}



	public Integer currentStock(Presentation aPresentation) {
		Integer ret = 0;
		
		if(this.stock.containsKey(aPresentation)){
			ret = this.stock.get(aPresentation);
		}
		return ret;
	}



	public void setMinimalStockForPresentation(Presentation aPresentation, int cant) {
		this.minStock.put(aPresentation, cant);		
	}



	public boolean isOnMinimalStock(Presentation aPresentation) {
		if(this.minStock.containsKey(aPresentation)){
			if (this.stock.containsKey(aPresentation)){
				return (this.stock.get(aPresentation) <= this.minStock.get(aPresentation));
			}
			return true;
		}
		return false;		
	}



	public void setCriticalStockForPresentation(Presentation aPresentation, int cant) {
		this.criticalStock.put(aPresentation, cant);		
	}



	public boolean isOnCriticalStock(Presentation aPresentation) {
		if(this.criticalStock.containsKey(aPresentation)){
			if (this.stock.containsKey(aPresentation)){
				return (this.stock.get(aPresentation) <= this.criticalStock.get(aPresentation));
			}
			return true;
		}
		return false;	
	}
	
	

}
